#include<bits/stdc++.h>
using namespace std;
const int MAN=1e3+5;
int N;
string op[MAN],str[MAN];
int flag[MAN];
bool check(int x){
	int tot=0,t2;
	for(int i=0;i<str[x].size();i++){
		if(str[x][i]=='.'){
			if(str[x][i+1]=='.') return false;
			tot++;
			if(tot>3) return false;
		}
		if(str[x][i]==':'){
			if(tot!=3) return false;
			t2++;
			if(t2==2) return false;
		}
	}
	int len=0,sum=0,j=str[x].size()-1;
	while(str[x][j]!=':'){
		len++;
		sum+=(str[x][j]-'0')*pow(10,len-1);
		j--;
	}
	if(len>5||sum>65535) return false;
	len=0,sum=0;
	for(int i=j-1;i>=0;i--){
		if(str[x][i]=='.'){len=0,sum=0;continue;}
		len++;
		sum+=(str[x][i]-'0')*pow(10,len-1);
		if(len>3||sum>255) return false;
	}
	return true;
}
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&N);
	for(int i=1;i<=N;i++){
		cin>>op[i]>>str[i];
		if(check(i)) flag[i]=1;
	}
	for(int i=1;i<=N;i++){
		if(flag[i]==0){printf("ERR\n");continue;}
		if(op[i]=="Server"){
			bool bz=true;
			for(int j=1;j<i;j++) if(op[i]==op[j]&&str[i]==str[j]){bz=false;break;}
			if(!bz) printf("FAIL\n");
			else printf("OK\n");
		}
		else{
			int ans=0;
			for(int j=1;j<i;j++) if(op[j]=="Server"&&str[j]==str[i]&&flag[j]){ans=j;break;}
			if(ans==0) printf("FAIL\n");
			else printf("%d\n",ans);
		}
	}
	return 0;
} 
